{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4a2750eb-b8a8-478d-9883-36ee61a0f5dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "from enum import Enum\n",
    "\n",
    "\n",
    "class Suite(Enum):\n",
    "    \"\"\"花色(枚举)\"\"\"\n",
    "    SPADE, HEART, CLUB, DIAMOND = range(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "10241d06-a202-42b8-ba46-6c51bf8ac09b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Suite.SPADE: 0\n",
      "Suite.HEART: 1\n",
      "Suite.CLUB: 2\n",
      "Suite.DIAMOND: 3\n"
     ]
    }
   ],
   "source": [
    "for suite in Suite:\n",
    "    print(f'{suite}: {suite.value}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cf1ccb3d-07b8-4621-b884-7927fa56744a",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Card:\n",
    "    \"\"\"牌\"\"\"\n",
    "\n",
    "    def __init__(self, suite, face):\n",
    "        self.suite = suite\n",
    "        self.face = face\n",
    "\n",
    "    def __repr__(self):\n",
    "        suites = '♠♥♣♦'\n",
    "        faces = ['', 'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']\n",
    "        # 根据牌的花色和点数取到对应的字符\n",
    "        return f'{suites[self.suite.value]}{faces[self.face]}'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "03380700-b8c8-4581-bff9-c2974101b704",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "♠5 ♥K\n"
     ]
    }
   ],
   "source": [
    "card1 = Card(Suite.SPADE, 5)\n",
    "card2 = Card(Suite.HEART, 13)\n",
    "print(card1, card2)    # ♠5 ♥K"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "dc4f043b-ce93-43e6-ba06-d442de7b9ba7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "\n",
    "class Poker:\n",
    "    \"\"\"扑克\"\"\"\n",
    "\n",
    "\n",
    "    def __init__(self):\n",
    "        # 通过列表的生成式语法创建一个装52张牌的列表\n",
    "        self.cards = [Card(suite, face) for suite in Suite\n",
    "                      for face in range(1, 14)]\n",
    "        # current属性表示发牌的位置\n",
    "        self.current = 0\n",
    "\n",
    "    def shuffle(self):\n",
    "        \"\"\"洗牌\"\"\"\n",
    "        self.current = 0\n",
    "        # 通过random模块的shuffle函数实现列表的随机乱序\n",
    "        random.shuffle(self.cards)\n",
    "\n",
    "    def deal(self):\n",
    "        \"\"\"发牌\"\"\"\n",
    "        card = self.cards[self.current]\n",
    "        self.current += 1\n",
    "        return card\n",
    "\n",
    "    @property\n",
    "    def has_next(self):\n",
    "        \"\"\"还有没有牌可以发\"\"\"\n",
    "        return self.current < len(self.cards)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4dc28970-09cc-4619-ab11-4881d7f5dcf0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[♥7, ♣K, ♣5, ♥J, ♦Q, ♣9, ♠2, ♥6, ♦5, ♣3, ♦9, ♦6, ♦7, ♦A, ♦3, ♣4, ♠4, ♦J, ♣A, ♥A, ♥4, ♥8, ♠Q, ♠A, ♣10, ♥3, ♥10, ♠7, ♦8, ♣Q, ♣8, ♥K, ♦2, ♣6, ♦4, ♦10, ♠K, ♥2, ♠8, ♠J, ♠6, ♥5, ♥9, ♠3, ♠5, ♠9, ♦K, ♥Q, ♣7, ♠10, ♣2, ♣J]\n"
     ]
    }
   ],
   "source": [
    "poker = Poker()\n",
    "poker.shuffle()\n",
    "print(poker.cards)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b3c97666-756f-414a-a240-10d648d64919",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Player:\n",
    "    \"\"\"玩家\"\"\"\n",
    "\n",
    "    def __init__(self, name):\n",
    "        self.name = name\n",
    "        self.cards = []\n",
    "\n",
    "    def get_one(self, card):\n",
    "        \"\"\"摸牌\"\"\"\n",
    "        self.cards.append(card)\n",
    "\n",
    "    def arrange(self):\n",
    "        self.cards.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b6111fc7-87e1-449e-b28f-c2f954eb37f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Card:\n",
    "    \"\"\"牌\"\"\"\n",
    "\n",
    "    def __init__(self, suite, face):\n",
    "        self.suite = suite\n",
    "        self.face = face\n",
    "\n",
    "    def __repr__(self):\n",
    "        suites = '♠♥♣♦'\n",
    "        faces = ['', 'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']\n",
    "        # 根据牌的花色和点数取到对应的字符\n",
    "        return f'{suites[self.suite.value]}{faces[self.face]}'\n",
    "    \n",
    "    def __lt__(self, other):\n",
    "        # 花色相同比较点数的大小\n",
    "        if self.suite == other.suite:\n",
    "            return self.face < other.face\n",
    "        # 花色不同比较花色对应的值\n",
    "        return self.suite.value < other.suite.value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "afa1f074-db2a-4463-862c-d7384a680d85",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
